Ext.define('CS.field.Combo', {
	extend: 'Ext.form.field.ComboBox',
	requires: ['CS.data.CustomStore'],

	minChars: 0,

	triggerAction: 'all',

	allQuery: '',

	queryDelay: 300,

	filterValid: false,

	constructor: function(config) {
		var me = this;

		config = CS.apply({}, config);

		config.isLoad = CS.getBoolean(config.isLoad, true);

		config.listeners = config.listeners || {};

		var fields = config.fields.split(',');
		config.valueField = fields[0];
		config.displayField = fields[1];

		if (CS.isNotEmpty(config.url)) {
			config.queryMode = 'remote';
			config.queryParam = config.valueField;

			config.store = Ext.create('CS.data.CustomStore', {
				url: config.url,
				fields: fields,
				autoLoad: config.isLoad,
				listeners: {
					beforeload: function(store, option) {
						option.params = option.params || {};
						CS.apply(option.params, me.extendParam);
					}
				}
			});

			if (config.selectAll) {
				var selectAll = {};
				selectAll[config.valueField] = '';
				selectAll[config.displayField] = '所有';

				config.store.addListener('load', function(store, records) {
					store.insert(0, selectAll);
				});
			}
		} else {
			config.queryMode = 'local';

			if (config.selectAll) {
				config.localData = ':所有,' + config.localData;
			}

			config.store = Ext.create('CS.data.CustomStore', {
				data: config.localData,
				fields: fields
			});
		}

		if (CS.isNotNull(config.value)) {
			config.value += '';
		}

		if (config.needTextTpl != false) {
			if (CS.isNull(config.textTpl)) {
				config.textTpl = '{' + config.displayField + '} ({' + config.valueField + '})';
			}
			config.listConfig = {
				getInnerTpl: function() {
					return config.textTpl;
				}
			};
		}

		config.listeners.blur = function() {
			if (CS.isEmpty(me.getDisplayValue()) && CS.isNotEmpty(me.getText())) {
				var store = me.picker.getStore();

				if (store.getCount() > 0) {
					me.setData(store.getAt(0).get(me.valueField));
				} else {
					me.clear();
				}
			}
		};

		me.callParent([config]);
	},

	doQuery: function(queryString, forceAll, rawQuery) {
		var me = this,

		queryPlan = me.beforeQuery({
			query: queryString || '',
			rawQuery: rawQuery,
			forceAll: forceAll,
			combo: me,
			cancel: false
		});

		if (queryPlan === false || queryPlan.cancel) {
			return false;
		}

		if (me.queryCaching && queryPlan.query === me.lastQuery) {
			me.expand();
		} else {
			me.lastQuery = queryPlan.query;
			me.doLocalQuery(queryPlan);
		}

		return true;
	},

	getText: function() {
		return this.getRawValue();
	},

	getTextByValue: function(value) {
		var me = this;

		var record = me.findRecord(me.valueField, value);
		return record ? record.get(me.displayField) : me.valueNotFoundText;
	},

	addParam: function(param) {
		if (Ext.isObject(param)) {
			CS.apply(this.extendParam, param);
		}
	},

	cleanParam: function() {
		for (var i = 0; i < arguments.length; i++) {
			delete this.extendParam[arguments[i]];
		}
	},

	cleanAllParam: function() {
		this.extendParam = {};
	},

	reload: function(params, callbackFn) {
		var me = this, newParams = params || {};

		me.reset();

		newParams[me.valueField] = '';

		me.store.load({
			params: params,
			callback: callbackFn
		});
	}

});
